﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class MyMessage : Form
    {
        #region -  EVENTS  -
        public event FormExitEventHandler OnFormExit = null;
        public event SendMessageEventHandler OnSendMessage = null;
        #endregion

        #region -  CONTRUCTOR  -
        public MyMessage()
        {
            InitializeComponent();
        }
        #endregion

        #region -  SEND MESSAGE  -
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (this.OnSendMessage != null)
            {
                this.OnSendMessage(this, new MessageEventArgs(this.tbxMessage.Text));
            }
            this.lbxListMessages.Items.Add("Tôi: " + this.tbxMessage.Text);
            this.tbxMessage.Text = null;
            this.tbxMessage.Focus();
        }
        #endregion

        #region -  SET MESSAGE  -
        public void SetMessage(string message)
        {
            if (this.lbxListMessages.InvokeRequired)
            {
                this.lbxListMessages.BeginInvoke(new MethodInvoker(
                    delegate()
                    {
                        this.SetMessage(message);
                    }));
            }
            else
            {
                this.lbxListMessages.Items.Add("Server: " + message);
            }
        }
        #endregion

        #region - FORM CLOSED  -
        private void MessageBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.OnFormExit != null)
            {
                this.OnFormExit(this, new FormExitEvenArgs());
            }
        }
        #endregion
    }
}
